package com.xyoto.takeawayUser.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xyoto.takeawayUser.constant.DataBasesOperationResult;
import com.xyoto.takeawayUser.constant.FileModelConstant;
import com.xyoto.takeawayUser.dto.SaveCarouselDto;
import com.xyoto.takeawayUser.entity.Carousel;
import com.xyoto.takeawayUser.enums.CarouselEnum;
import com.xyoto.takeawayUser.exception.ExceptionUtils;
import com.xyoto.takeawayUser.mapper.CarouselMapper;
import com.xyoto.takeawayUser.service.CarouselService;
import com.xyoto.takeawayUser.utils.MinioUtils;
import com.xyoto.takeawayUser.utils.Result;
import com.xyoto.takeawayUser.vo.CarouselVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: 轮播图Service接口
 * @Author: 挽风
 * @Date: 2022
 * @Copyright: 2022 by 挽风
 * @Description:
 **/

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {


    @Autowired
    private CarouselMapper carouselMapper;

    @Autowired
    private MinioUtils minioUtils;

    /**
     * 新增轮播图
     * @param saveDto
     * @return
     */
    @Override
    public Result saveCarousel(SaveCarouselDto saveDto) {
        if (saveDto.getOrder() == null || saveDto.getOrder() <= 0){
            ExceptionUtils.error(CarouselEnum.SHOW_ORDERS_ERROR);
        }
        if (saveDto.getFile() == null || saveDto.getFile().getSize() == 0){
            log.info("——文件读取失败——" + saveDto.getFile().getOriginalFilename());
            ExceptionUtils.error(CarouselEnum.NOT_CAROUSEL_FILE);
        }
        // 上传文件
        String path = null;
        try {
            path = minioUtils.uploadFile(saveDto.getFile(), FileModelConstant.TA_CAROUSEL);
            if (StrUtil.isEmpty(path)){
                ExceptionUtils.error(CarouselEnum.SAVE_CAROUSEL_FAIL);
            }
        } catch (Exception e) {
            log.info("——文件上传失败——");
            e.printStackTrace();
            ExceptionUtils.error(CarouselEnum.SAVE_CAROUSEL_FAIL);
        }

        // 处理顺序
        Integer maxOrder =  carouselMapper.getMaxOrder();
        // 查询是否已存在当前顺序
        Integer order = saveDto.getOrder();
        if (order != null){
            Integer isFlag = carouselMapper.judgeOrderExist(order);
            if (isFlag != 0){
                order = maxOrder + 1;
            }else{
                order = maxOrder;
            }
        }

        Carousel carousel = new Carousel();
        carousel.setPath(path);
        carousel.setOrder(order);
        carousel.setLink(saveDto.getLink());
        carousel.setStatus(saveDto.getStatus());

        Integer saveFlag = carouselMapper.saveCarousel(carousel);
        if (saveFlag == null || saveFlag == DataBasesOperationResult.ERROR){
            log.info("——轮播图添加失败——" + saveFlag);
            ExceptionUtils.error(CarouselEnum.SAVE_CAROUSEL_FAIL);
        }
        return Result.success();
    }

    /**
     * 获取所有轮播图
     * @return
     */
    @Override
    public Result getCarouselAll() {
        List<CarouselVo> carouselVoList =  carouselMapper.getCarouselAll();
        return Result.success(carouselVoList);
    }
}
